clear all
set more off


***Put together inflation data
clear
insheet using "$inputs/deflators/Inflation_India_column_only.csv"
rename inflationgdpdeflatorannual inflation
replace inflation = inflation/100
save "$working/inflation", replace

***Put together Interest rate data
clear
insheet using "$inputs/deflators/Interest_Rates_column_only.csv"
replace interest_rate = interest_rate/100
save "$working/interest", replace

clear

use "$working/inflation"
merge 1:1 year using "$working/interest"
drop _merge

drop if year < 1988

drop if year > 2018

generate real_interest = (interest_rate - inflation)/(1+ inflation)

save "$working/real_interest_rate", replace


**Put together wholesale price index
clear

insheet using "$inputs/deflators/wholesale_price_index_newest.csv"

rename wholesalepriceindex2010100 wholesale_priceindex

save "$working/wholesale_priceindex", replace

clear

use "$working/prowess_clean", clear

**Some notes: net freehold and leasehold land almost always sums up to net land
keep products_cocode1 prod_date3 sa_gross_fixed_asset_2206 sa_net_land sa_land sa_land_addn sa_land_addn_reval sa_land_deduct ///
sa_net_building sa_building sa_building_addn sa_building_addn_rev_2262 sa_building_deduct ///
sa_net_plant sa_plant sa_plant_addn sa_plant_addn_reval sa_plant_deduct ///
sa_net_computer_it sa_computer_it sa_computer_it_addn sa_computer_it_addn__2283 sa_computer_it_deduc_2284 ///
sa_transport_comm_eq_2300 sa_net_transport_inf_2301 sa_transport_infra sa_transport_infra_a_2303 sa_transport_infra_a_2304 sa_transport_infra_d_2305 sa_net_transport_veh_2308 ///
 sa_transport_vehicle_2309 sa_transport_vehicle_2310 sa_transport_vehicle_2311 sa_transport_vehicle_2312 sa_net_comm_equip sa_comm_equip sa_comm_equip_addn sa_comm_equip_addn_r_2318 sa_comm_equip_deduct_2319 ///
 sa_gross_fixed_ast_a_2356 sa_gross_fixed_ast_a_2357 sa_gross_fixed_ast_d_2358 sa_net_fixed_assets
 
rename sa_gross_fixed_asset_2206 gross_fixed_assets_all

**Land
rename sa_net_land net_land
rename sa_land gross_land
rename sa_land_addn land_add
rename sa_land_addn_reval land_reval
rename sa_land_deduct land_deduc

**buildings
rename sa_net_building net_building
rename sa_building gross_building
rename sa_building_addn building_add
rename sa_building_addn_rev_2262 building_reval
rename sa_building_deduct building_deduc

**machinery
rename sa_net_plant net_pl_machinery
rename sa_plant gross_pl_machinery
rename sa_plant_addn pl_machinery_add
rename sa_plant_addn_reval pl_machinery_reval
rename sa_plant_deduct pl_machinery_deduc

**computers and software
rename sa_net_computer_it net_computer
rename sa_computer_it gross_computer
rename sa_computer_it_addn computer_add
rename sa_computer_it_addn__2283 computer_reval
rename sa_computer_it_deduc_2284 computer_deduc

*** transportation equipment 
rename sa_transport_comm_eq_2300 gross_trans_comm_inf

rename sa_net_transport_inf_2301 net_trans_inf 
rename sa_transport_infra gross_net_trans_inf
rename sa_transport_infra_a_2303 trans_inf_add
rename sa_transport_infra_a_2304 trans_inf_reval
rename sa_transport_infra_d_2305 trans_inf_deduc

rename sa_net_transport_veh_2308 net_trans_equip
rename sa_transport_vehicle_2309 gross_trans_equip
rename sa_transport_vehicle_2310 trans_equip_add
rename sa_transport_vehicle_2311 trans_equip_reval
rename sa_transport_vehicle_2312 trans_equip_deduc

rename sa_net_comm_equip net_comm
rename sa_comm_equip gross_comm
rename sa_comm_equip_addn comm_add
rename sa_comm_equip_addn_r_2318 comm_reval
rename sa_comm_equip_deduct_2319 comm_deduc

rename sa_gross_fixed_ast_a_2356  gross_fixed_assets_all_add
rename sa_gross_fixed_ast_a_2357 gross_fixed_assets_all_reval 
rename sa_gross_fixed_ast_d_2358 gross_fixed_assets_all_deduc

rename sa_net_fixed_assets net_fix_assets_all

collapse (mean) gross_fixed_assets_all net_land gross_land land_add land_reval land_deduc /// 
net_building gross_building building_add building_reval building_deduc net_pl_machinery ///
gross_pl_machinery pl_machinery_add pl_machinery_reval pl_machinery_deduc net_computer ///
gross_computer computer_add computer_reval computer_deduc gross_trans_comm_inf /// 
net_trans_inf gross_net_trans_inf trans_inf_add trans_inf_reval trans_inf_deduc ///
net_trans_equip gross_trans_equip trans_equip_add trans_equip_reval trans_equip_deduc ///
net_comm gross_comm comm_add comm_reval comm_deduc gross_fixed_assets_all_add ////
gross_fixed_assets_all_reval gross_fixed_assets_all_deduc net_fix_assets_all, by(products_cocode1 prod_date3)

save "$working/asset_data", replace


clear 
use "$clean/prowess_wits_IO.dta", clear

generate co_code1 = products_cocode1 
generate date_string = prod_date3 
generate year2  = substr(date_string,1,4)
generate month2  = substr(date_string,5,2)
generate day2  = substr(date_string,7,2)

keep if ann_rep_months4==12 // Keep firms reporting data for 12 months.
keep if month2=="03" 

destring year, replace

keep products_cocode1 co_code prod_date3 year

bysort products_cocode1 prod_date3 year: generate count_thing = _n
keep if count_thing==1
drop count_thing

isid products_cocode1 year
tempfile temp1 

save `temp1', replace

clear

use "$working/asset_data"

merge 1:1 products_cocode1 prod_date3 using `temp1'

drop if _merge==1
drop _merge

**Define depreciation rates from Boehm and Oberfield (2018)
gen deprec_land=0
gen deprec_build=0.05
gen deprec_plant=0.1
gen deprec_trans=0.2
gen deprec_comp=0.4

merge m:1 year using "$working/wholesale_priceindex"
drop if _merge==2
drop _merge

*Instead of 100, make base year 1 (need to deflate to put all investment/capital stocks in the same base year rupees)
replace wholesale_priceindex = wholesale_priceindex/100 

*keep track of real missings for starting values:
generate svalue_missing = 0
replace svalue_missing = 1 if gross_fixed_assets_all==.

generate svalue_missing_micro = 0
replace svalue_missing_micro = 1 if gross_land==. & gross_building==. & gross_pl_machinery==. & gross_computer==. & gross_net_trans_inf==. & gross_trans_equip==. & gross_comm==. // Need ALL missing to be true missing; otherwise we can keep track of some starting value


xtset co_code1 year

foreach variable in gross_fixed_assets_all net_land gross_land land_add land_reval land_deduc net_building gross_building building_add building_reval building_deduc net_pl_machinery gross_pl_machinery pl_machinery_add pl_machinery_reval pl_machinery_deduc net_computer gross_computer computer_add computer_reval computer_deduc gross_trans_comm_inf net_trans_inf gross_net_trans_inf trans_inf_add trans_inf_reval trans_inf_deduc net_trans_equip gross_trans_equip trans_equip_add trans_equip_reval trans_equip_deduc net_comm gross_comm comm_add comm_reval comm_deduc gross_fixed_assets_all_add gross_fixed_assets_all_reval gross_fixed_assets_all_deduc net_fix_assets_all {
	replace `variable' = 0 if `variable'==.
}

tabulate year


**Define investment
generate inv_base = (gross_fixed_assets_all_add + gross_fixed_assets_all_reval - gross_fixed_assets_all_deduc)/wholesale_priceindex
**Missing until filled in as we accumulate forward
generate K_base = .

**Base K for starting year
replace K_base = (gross_fixed_assets_all)/wholesale_priceindex if year==1988 & svalue_missing !=1

forvalues i = 1989/2018 {
	
	*in the following year; apply law of motion if lagged year not missing for capital stock (if no investment, we treat that as zero investment)
	replace K_base = (1-0.12)*L.K_base + L.inv_base if year==`i' 
	*If K_base not updated this year, we now feed in the K_base value for this year
	replace K_base = (gross_fixed_assets_all)/wholesale_priceindex if year==`i' & K_base==. & svalue_missing !=1

}

sum K_base
generate inv_land = (land_add + land_reval - land_deduc)/wholesale_priceindex
generate K_land = .
replace K_land = (gross_land)/wholesale_priceindex if year==1988 & svalue_missing_micro!=1

generate inv_building = (building_add + building_reval - building_deduc)/wholesale_priceindex
generate K_building = .
replace K_building = (gross_building)/wholesale_priceindex if year==1988 & svalue_missing_micro!=1

generate inv_pl_machinery = (pl_machinery_add + pl_machinery_reval - pl_machinery_deduc)/wholesale_priceindex
generate K_pl_machinery = .
replace K_pl_machinery = (gross_pl_machinery)/wholesale_priceindex if year==1988 & svalue_missing_micro!=1

generate inv_computer = (computer_add + computer_reval - computer_deduc)/wholesale_priceindex
generate K_computer = .
replace K_computer = (gross_computer)/wholesale_priceindex if year==1988 & svalue_missing_micro!=1

generate inv_trans = (trans_inf_add  +trans_inf_reval - trans_inf_deduc + trans_equip_add + trans_equip_reval - trans_equip_deduc + comm_add + comm_reval - comm_deduc)/wholesale_priceindex
generate K_trans = .
replace K_trans = (gross_net_trans_inf + gross_trans_equip + gross_comm)/wholesale_priceindex if year==1988 & svalue_missing_micro!=1

**Same process by by type of capital
forvalues i = 1989/2018 {

	replace K_land = (1-deprec_land)*L.K_land + L.inv_land if year==`i' 
	replace K_land = (gross_land)/wholesale_priceindex if year==`i' & K_land==. & svalue_missing_micro!=1
	
	replace K_building = (1-deprec_build)*L.K_building + L.inv_building if year==`i' 
	replace K_building = (gross_building)/wholesale_priceindex if year==`i' & K_building==. & svalue_missing_micro!=1
	
	replace K_pl_machinery = (1-deprec_plant)*L.K_pl_machinery + L.inv_pl_machinery if year==`i' 
	replace K_pl_machinery = (gross_pl_machinery)/wholesale_priceindex if year==`i' & K_pl_machinery==. & svalue_missing_micro!=1
	
	replace K_computer = (1-deprec_comp)*L.K_computer + L.inv_computer if year==`i' 
	replace K_computer = (gross_computer)/wholesale_priceindex if year==`i' & K_computer==. & svalue_missing_micro!=1
	
	replace K_trans = (1-deprec_trans)*L.K_trans + L.inv_trans if year==`i' 
	replace K_trans = (gross_net_trans_inf + gross_trans_equip + gross_comm)/wholesale_priceindex if year==`i' & K_trans==. & svalue_missing_micro!=1

}

generate micro_K = K_land + K_building + K_pl_machinery + K_computer + K_trans
generate micro_inv = inv_land + inv_building + inv_pl_machinery + inv_computer + inv_trans 

preserve
	clear
	use "$working/real_interest_rate"
	sum real_interest
	local real_interest = r(mean)
restore

*We are using a single real interest rate, averaged over the entire time period, as in Raval (2022)
generate real_interest = `real_interest'

generate rK = K_land*(real_interest + deprec_land) + K_building*(real_interest + deprec_build) + K_pl_machinery*(real_interest + deprec_plant) + K_computer*(real_interest + deprec_comp) + K_trans*(real_interest + deprec_trans)

keep co_code1 year K_base inv_base inv_land K_land inv_building K_building inv_pl_machinery K_pl_machinery inv_computer K_computer inv_trans K_trans micro_K micro_inv rK real_interest

save "$working/perpetual_inventory_capital", replace
